﻿using System.Collections.Generic;
using Sustainalytics.SharedEntities.EsgResearch;
using Sustainalytics.Synchronization.Interface;
using System.Linq;
using System.Diagnostics;
using Sustainalytics.Entities;

namespace Sustainalytics.ClientDW.Synchronization.SyncESGData.IndicatorTemplateLogic
{
    public class IndicatorTemplateQuery : IComprehensiveEsgIndicatorTemplateQuery
    {
        private readonly ILogger logger;
        private readonly BackOfficeAppSettings backOfficeAppSettings;
        private readonly ResearchDataProvider.ResearchDataProvider researchDataProvider;

        public IndicatorTemplateQuery(ResearchDataProvider.ResearchDataProvider researchDataProvider, BackOfficeAppSettings backOfficeAppSettings, ILogger logger)
        {
            this.researchDataProvider = researchDataProvider;
            this.logger = logger;
            this.backOfficeAppSettings = backOfficeAppSettings;

            logger.Log($"\nWorking with HBase Thrift server {backOfficeAppSettings.ThriftServer} and Phoenix server {backOfficeAppSettings.PhoenixServer}");
        }

        List<IndicatorTemplate> IComprehensiveEsgIndicatorTemplateQuery.Get()
        {
            var callerMethod = new StackFrame(1).GetMethod().Name;

            var stopWatch = Stopwatch.StartNew();

            var indicatorTemplates = Retry.Do(() => researchDataProvider.GetIndicatorTemplate(ResearchDataProvider.Models.Analyst_Interface.FrameworkCodes.ComprehensiveRatings).ToList(),
                                                 backOfficeAppSettings.WaitRetryDuration,
                                                 logger,
                                                 backOfficeAppSettings.RetryNumberOfTimes);
            logger.Log($"Received {indicatorTemplates.Count} indicator templates from the cluster for {callerMethod} (duration {stopWatch.Elapsed})");

            return indicatorTemplates;
        }
    }
}
